/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package view;

import control.GuiManager;
import control.SettingsManager;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author Jojo
 */
public class BooOptionsDialog extends javax.swing.JDialog {

    GuiManager guiMan;
    final JFileChooser fc;
    
    
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();
    /**
     * Creates new form LejosOptionsDialog
     */
    public BooOptionsDialog(java.awt.Frame parent, boolean modal,GuiManager pGuiMan) {
        super(parent, modal);
        initComponents();
        this.guiMan = pGuiMan;
        this.btnGroupMonoOrNet.add(radioMono);
        this.btnGroupMonoOrNet.add(radioNet);        
        fc = new JFileChooser();
        this.setLocationRelativeTo(getParent());
        this.readConfigAndUpdateView();
        this.setResizable(false);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupMonoOrNet = new javax.swing.ButtonGroup();
        backgroundPanel = new javax.swing.JPanel();
        lbl_pathToBoo = new javax.swing.JLabel();
        txtfld_pathToBoo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btn_pathToBoo = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 2), new java.awt.Dimension(0, 2), new java.awt.Dimension(32767, 2));
        lblChooseMonoOrNot = new javax.swing.JLabel();
        pnlMonoOrNet = new javax.swing.JPanel();
        radioMono = new javax.swing.JRadioButton();
        radioNet = new javax.swing.JRadioButton();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 2), new java.awt.Dimension(0, 2), new java.awt.Dimension(32767, 2));
        lbl_pathToUploader = new javax.swing.JLabel();
        txtfld_pathToMono = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btn_pathToUploader = new javax.swing.JButton();
        filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 10), new java.awt.Dimension(0, 10), new java.awt.Dimension(32767, 10));
        jPanel1 = new javax.swing.JPanel();
        btn_close = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(100, 200));

        backgroundPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        backgroundPanel.setLayout(new java.awt.GridLayout(12, 1, 0, 3));

        lbl_pathToBoo.setText(langBundle.getString("BooOptionsLblBooPath"));
        backgroundPanel.add(lbl_pathToBoo);

        txtfld_pathToBoo.setText("/directory/file");
        txtfld_pathToBoo.setMaximumSize(new java.awt.Dimension(800, 25));
        backgroundPanel.add(txtfld_pathToBoo);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        btn_pathToBoo.setText(langBundle.getString("BooOptionsBtnBooPath"));
        btn_pathToBoo.setMaximumSize(new java.awt.Dimension(1033, 1023));
        btn_pathToBoo.setPreferredSize(null);
        btn_pathToBoo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_pathToBooActionPerformed(evt);
            }
        });
        jPanel3.add(btn_pathToBoo);

        backgroundPanel.add(jPanel3);
        backgroundPanel.add(filler1);

        lblChooseMonoOrNot.setText(langBundle.getString("MonoOrNetLabel"));
        backgroundPanel.add(lblChooseMonoOrNot);

        java.awt.FlowLayout flowLayout1 = new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 1);
        flowLayout1.setAlignOnBaseline(true);
        pnlMonoOrNet.setLayout(flowLayout1);

        radioMono.setText("Mono (default)");
        radioMono.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                handleMonoStateChange(evt);
            }
        });
        pnlMonoOrNet.add(radioMono);

        radioNet.setText("Microsoft .Net");
        pnlMonoOrNet.add(radioNet);

        backgroundPanel.add(pnlMonoOrNet);
        backgroundPanel.add(filler2);

        lbl_pathToUploader.setText(langBundle.getString("BooOptionsLblMonoPath"));
        backgroundPanel.add(lbl_pathToUploader);

        txtfld_pathToMono.setText("/directory/file");
        txtfld_pathToMono.setMaximumSize(new java.awt.Dimension(800, 25));
        backgroundPanel.add(txtfld_pathToMono);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        btn_pathToUploader.setText(langBundle.getString("BooOptionsBtnMonoPath"));
        btn_pathToUploader.setMaximumSize(new java.awt.Dimension(1033, 1023));
        btn_pathToUploader.setPreferredSize(null);
        btn_pathToUploader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_pathToUploaderActionPerformed(evt);
            }
        });
        jPanel2.add(btn_pathToUploader);

        backgroundPanel.add(jPanel2);
        backgroundPanel.add(filler6);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 3));

        btn_close.setText(langBundle.getString("BooEditorOptionsBtnClose"));
        btn_close.setMaximumSize(new java.awt.Dimension(1033, 1023));
        btn_close.setPreferredSize(null);
        btn_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_closeActionPerformed(evt);
            }
        });
        jPanel1.add(btn_close);

        backgroundPanel.add(jPanel1);

        getContentPane().add(backgroundPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_pathToBooActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_pathToBooActionPerformed

        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.txtfld_pathToBoo.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_btn_pathToBooActionPerformed

    private void btn_pathToUploaderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_pathToUploaderActionPerformed

        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.txtfld_pathToMono.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_btn_pathToUploaderActionPerformed

    private void btn_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_closeActionPerformed
        saveEditorConfig();
        this.guiMan.updateGui();
        this.dispose();
    }//GEN-LAST:event_btn_closeActionPerformed

    private void handleStateChange(){
        if(this.radioMono.isSelected()){
            this.radioNet.setSelected(false);
            this.txtfld_pathToMono.setEditable(true);
            this.txtfld_pathToMono.setEnabled(true);
            this.btn_pathToUploader.setEnabled(true);
            this.lbl_pathToUploader.setEnabled(true);
        }
        else{
            this.radioNet.setSelected(true);
            this.txtfld_pathToMono.setEditable(false);
            this.txtfld_pathToMono.setEnabled(false);
            this.btn_pathToUploader.setEnabled(false);
            this.lbl_pathToUploader.setEnabled(false);
                 
        }
    }
    private void handleMonoStateChange(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_handleMonoStateChange
        this.handleStateChange();
    }//GEN-LAST:event_handleMonoStateChange

    private void readConfigAndUpdateView(){
        this.txtfld_pathToBoo.setText(SettingsManager.getStringSettingValue("PATH_TO_BOO"));
        this.txtfld_pathToMono.setText(SettingsManager.getStringSettingValue("PATH_TO_MONO"));
        if(SettingsManager.getBooleanSettingValue("MONO")){
            this.radioMono.setSelected(true);
            this.radioNet.setSelected(false);
        }
        else{
            this.radioMono.setSelected(false);
            this.radioNet.setSelected(true);
        }
        this.handleStateChange();
        
    }
    
    private void saveEditorConfig(){
        SettingsManager.updateSettingsItem("PATH_TO_BOO", this.txtfld_pathToBoo.getText());
        SettingsManager.updateSettingsItem("PATH_TO_MONO", txtfld_pathToMono.getText());
        if(this.radioMono.isSelected()){
            SettingsManager.updateSettingsItem("MONO", "true");
        }
        else{
            SettingsManager.updateSettingsItem("MONO", "false");
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.ButtonGroup btnGroupMonoOrNet;
    private javax.swing.JButton btn_close;
    private javax.swing.JButton btn_pathToBoo;
    private javax.swing.JButton btn_pathToUploader;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblChooseMonoOrNot;
    private javax.swing.JLabel lbl_pathToBoo;
    private javax.swing.JLabel lbl_pathToUploader;
    private javax.swing.JPanel pnlMonoOrNet;
    private javax.swing.JRadioButton radioMono;
    private javax.swing.JRadioButton radioNet;
    private javax.swing.JTextField txtfld_pathToBoo;
    private javax.swing.JTextField txtfld_pathToMono;
    // End of variables declaration//GEN-END:variables
}
